/**
 * 获取任意时间
 */
 const getDate=function (date, AddDayCount = 0) {
	if (!date) {
		date = new Date()
	}
	if (typeof date !== 'object') {
		date = date.replace(/-/g, '/')
	}
	const dd = new Date(date)

	dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

	const y = dd.getFullYear()
	const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
	const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
	return {
		fullDate: y + '-' + m + '-' + d,
		year: y,
		month: m,
		date: d,
		day: dd.getDay()
	}
}
 /**
  * 计算两个日期之间的天数差
  * @param {string} serviceStartTime - 服务开始时间，格式应为'YYYY-MM-DD'
  * @param {string} serviceEndTime - 服务结束时间，格式应为'YYYY-MM-DD'
  * @returns {number|string} 返回两个日期之间的天数差如果输入日期格式无效或缺失，则返回字符串"0"
  */
 const calculateDaysBetweenDates=function(serviceStartTime,serviceEndTime) {
	 if(!serviceStartTime || !serviceEndTime)return "0"
    const startDate = new Date(serviceStartTime);
    const endDate = new Date(serviceEndTime);

    // Check if dates are valid
    if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
      console.error('Invalid date format');
      return;
    }

    // Calculate the difference in milliseconds
    const diffInMs = Math.abs(endDate - startDate);

    // Convert milliseconds to days
    let diffInDays = (diffInMs / (1000 * 60 * 60 * 24))+1;

    // If the dates are the same, consider it as 1 day
    if (diffInDays === 0) {
      diffInDays = 1;
    }

    return diffInDays;
}
// 将时间字符串转换为小数表示的分钟数
const timeToDecimal=function(timeStr) {
  const [hours, minutes] = timeStr.split(':').map(Number);
  return hours + (minutes / 60);
}


/**
 * 获取周期内指定星期数量
 * @param {Date} startDate - 开始时间
 * @param {Date} endDate - 结束时间
 * @param {Array<string>} daysOfWeek - 需要计算的星期
 * @returns {number} - 天数
 * @author woods
 * @date 2024-08-05 14:45:44
 */
const countSpecificWeekdays=function(startDate, endDate, daysOfWeek) {
  let total = 0;

  let currentDate = new Date(startDate);

  while (currentDate <= endDate) {
    const dayOfWeekIndex = currentDate.getDay();
    const dayName = getDayName(dayOfWeekIndex);

    if (daysOfWeek.includes(dayName)) {
      total++;
    }

    currentDate.setDate(currentDate.getDate() + 1);
  }

  return total;
}

function getDayName(dayIndex) {
  const dayNames = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  return dayNames[dayIndex];
}
//  结束时间 是否大于等于开始时间后的2小时
const isEndTimeValid=function(startTimeStr, endTimeStr) {
  // 创建一个基准日期，用于构建完整的日期时间对象
  const baseDate = new Date();
  
  // 分割时间字符串并提取小时和分钟
  const [startHours, startMinutes] = startTimeStr.split(':').map(Number);
  const [endHours, endMinutes] = endTimeStr.split(':').map(Number);

  // 创建开始时间和结束时间的 Date 对象
  const startTime = new Date(baseDate.getFullYear(), baseDate.getMonth(), baseDate.getDate(), startHours, startMinutes);
  const endTime = new Date(baseDate.getFullYear(), baseDate.getMonth(), baseDate.getDate(), endHours, endMinutes);

  // 计算开始时间之后2小时的时间戳
  const twoHoursLater = new Date(startTime.getTime() + 2 * 60 * 60 * 1000); // 2 hours in milliseconds
  
  // 比较结束时间和开始时间之后2小时的时间戳
  return endTime >= twoHoursLater;
}
 

// 用于筛选并找到最优优惠券的方法
const findBestCoupon=function(coupons, totalPrice) {
  // 筛选出所有可以使用的优惠券
  const applicableCoupons = coupons.filter(coupon => coupon.usePrice <= totalPrice);

  // 如果没有适用的优惠券，则返回 null 或者提示信息
  if (applicableCoupons.length === 0) {
    return null;
  }

  // 从适用的优惠券中找出满减金额最高的优惠券
  const bestCoupon = applicableCoupons.reduce((best, current) => {
    return (best.discountPrice > current.discountPrice) ? best : current;
  }, applicableCoupons[0]);

  return bestCoupon;
}
export {
	getDate,calculateDaysBetweenDates,findBestCoupon,timeToDecimal,countSpecificWeekdays,isEndTimeValid
}